﻿using Foothill.IoC;
using Foothill.Service.Configuration;
using Foothill.Service.UserAccount;
using System;
using System.Web;
using System.Web.Security;

namespace Foothill.Web.MasterPage
{
    public partial class NoRightAd : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            DisplayLoginPanelContent();
            SetupFooter();
        }

        protected void lbtSignOutClick(object sender, EventArgs e)
        {
            IAuthenticationService authService = IocContainer.GetInstance<IAuthenticationService>();
            FormsAuthentication.SignOut();
            Session.Abandon();

            // clear authentication cookie
            HttpCookie cookie = authService.GetAuthenticationCookieForSignOut();
            Response.Cookies.Add(cookie);

            Response.Redirect("~/ForSaleList/ForSaleList.aspx");
        }

        // show/hide content based on whether user is authenticated
        private void DisplayLoginPanelContent()
        {
            if (HttpContext.Current.User.Identity.IsAuthenticated)
            {
                // authenticated user, display welcome 
                pnlLogin.Visible = false;
                pnlWelcome.Visible = true;
                SetupWelcomePanel();
            }
            else
            {
                // display login 
                pnlLogin.Visible = true;
                pnlWelcome.Visible = false;
            }
        }

        private void SetupWelcomePanel()
        {
            hlkWelcome.Text = HttpContext.Current.User.Identity.Name;
            hlkWelcome.NavigateUrl = "~/UserProfile/User.aspx";
        }

        private void SetupFooter()
        {
            IHostSettingManager hostSettingManager = IocContainer.GetInstance<IHostSettingManager>();
            LblDbName.Text = hostSettingManager.GetConnectedDatabaseTypeName();
        }
    }
}